// state.js

// 区间生成随机数
const rand = require("../js/functions.js").rand
// 中央集中型（纺缍型）区间生成随机数
const mrand = require("../js/functions.js").mrand
// 预设数据
const datas = require("../res/datas.js")

const State = {
	areaTypes: datas.areaTypes,
	healthTypes: datas.healthTypes,
	
	createNew: function(id){
		const p = {
			_proto: State, 																	// 定义原形
			id: id, stateName: "", type: 0, typeName: "", belong: id < 10 ? 0 : id,			// 名称id
			labours: 0,  weaks: 0,   														// 劳动力与非劳动力
			area: 0, farm: 0, herd: 0, town: 0, forest: 0, mine: 0, 						// 土地面积分布，以及矿储量
			money: 0, honest: 0, 															// 民资总额，民忠
			gold: 0, rice: 0, wood: 0, iron: 0, bow: 0, sword: 0, armour: 0, horse: 0,		// 官储物资
		}
		p.desert = function(){
			return this.area - this.herd - this.farm - this.town - this.forest
		}
		p.jobs = function(){
			return this.herd * 1 + this.farm * 4 + this.town * 20 + this.forest * 1
		}
		p.maxPerProduceRate = function(){ // 最大单位生产率，为民忠2倍。民忠即为肯好好劳动的劳动力的比率。每个劳动力可以操作2块地。
			return Math.floor(this.honest * 2)
		}
		p.perProduceRate = function(){ // 单位生产率，
			return Math.min(this.jobs / this.labours * 100, this.maxPerProduceRate)
		}
		p.produce = function(){ // 产量
			return this.perProduceRate * this.labours * 100
		}
		p.health = function(){
			let k = this.money / (this.labours + this.weaks)
			let h = -1;
			for(let i in State.healthTypes){
				let x = State.healthTypes[i]
				if(k >= x.minMoney ) h ++  // this.money不允许为负，则k不为负，h++ 将至少执行一次。
			}
			return h
		}
		p.healthName = function(){
			return State.healthTypes[p.health].name
		}


		return p
	},

	init : function(){
		const statesdatas = datas.statesdatas
		const states = []

		for( let i in statesdatas){
			let x = statesdatas[i]
			const p = State.createNew(i)
			p.stateName = x.name
			p.type = x.type
			p.typeName = State.areaTypes[x.type].name

			// 初始化人口
			p.labours = Math.floor(x.popu * 0.25)
			p.weaks = x.popu - p.labours
			p.honest = Math.floor(rand(35, 45))
			p.money = Math.floor(rand(1, 5) * x.popu )			

			// 初始化土地面积
			let t = State.areaTypes[x.type]
			p.forest = x.area * (rand(0, 5) + t.forestBase) / 100
			p.herd = x.area * (rand(0, 5) + t.herdGrow) / 100
			p.farm = x.area * (rand(0, 5) + t.farmGrow) / 100
			p.town = x.area * (rand(0, 5) + t.townGrow) / 100
			p.mine = x.area * (rand(0, 5) + t.mineGrow) / 100

			p.gold = 3000000
			p.rice = 3000
			p.wood = 200
			p.iron = 100
			// 其他几项，初始值为0，不再初始化。


			states.push(p)

		}

		return states
	}




}


module.exports.State = State